﻿using System;
using System.Windows.Forms;
using SdmxMl.Structure;

namespace SdmxMl.UC
{
    public partial class UcOrganisation : UcBase
    {
        private OrganisationType ot = null;

        public UcOrganisation()
        {
            InitializeComponent();
            customComboBoxName.DropDownControl = ucMlText;
            
        }

        public override bool BindFromNode(TreeNode node, bool isFinal)
        {
            base.BindFromNode(node, isFinal);
            if (node != null)
                ot = node.Tag as OrganisationType;
            BindToControls(isFinal);
            IsFinalEditControls(isFinal);
            return Visible;
        }

        private void BindToControls(bool isFinal)
        {
            if (ot != null)
            {
                Visible = true;

                textBoxId.Text = ot.Id;
                FillParentInfo(textBoxParent, textBoxParentCodeDescr);
                textBoxVersion.Text = ot.Version;
                textBoxUrn.Text = ot.Urn;
                checkBoxIsExtRef.Checked = ot.IsExternalReference;
                textBoxValidFrom.Text = ot.ValidFrom;
                textBoxValidTo.Text = ot.ValidTo;
                customComboBoxName.Text = ot.Name.ToString();
                ucMlText.ML = ot.Name;
                comboMultiLinesDescr.Text = ot.Description.ToString();
                comboMultiLinesDescr.ML = ot.Description;
                UpdateContactOverview();
                ucContactType.BindFromNode(ot, isFinal);
                
            }
            else
                Visible = false;
        }

        protected override bool BindFromControls()
        {
            bool err = false;
            if (ot != null)
            {
                err = CheckUniqueId(textBoxId);
                ot.Version = textBoxVersion.Text;
                ot.Urn = textBoxUrn.Text;
                //ot.URI = textBoxUri.Text;
                ot.ValidFrom = textBoxValidFrom.Text;
                ot.ValidTo = textBoxValidTo.Text;
                ot.Name.UpdateForCurrentLanguage(customComboBoxName.Text);
                ot.Description.UpdateForCurrentLanguage(comboMultiLinesDescr.Text);
                ucContactType.UpdateNodeTag();

                // Update tree img if needed
                int imgIndex = ot.UsedImageIndex;
                if (imgIndex != treeNode.ImageIndex)
                    treeNode.ImageIndex = treeNode.SelectedImageIndex = imgIndex;
            }
            return err;
        }

        // No edition if Parent Artefact IsFinal
        private void IsFinalEditControls(bool isFinal)
        {
            textBoxId.ReadOnly = isFinal;
            textBoxVersion.ReadOnly = isFinal;
            textBoxUrn.ReadOnly = isFinal;
            textBoxValidFrom.ReadOnly = isFinal;
            textBoxValidTo.ReadOnly = isFinal;
            textBoxVersion.ReadOnly = isFinal;
            customComboBoxName.IsFinal = isFinal;
            comboMultiLinesDescr.IsFinal = isFinal;
            checkBoxIsExtRef.Enabled = isFinal == false;
        }


        private void tabControlOrg_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (tabControlOrg.SelectedIndex == 0)
            {
                ucContactType.UpdateNodeTag();
                UpdateContactOverview();
            }

        }

        private void UpdateContactOverview()
        {
            ucContactOverviewMaintenance.CT = ot.MaintenanceContact;
            ucContactOverviewDisseminator.CT = ot.DisseminatorContact;
            ucContactOverviewCollector.CT = ot.CollectorContact;
            ucContactOverviewReporter.CT = ot.ReporterContact;
            ucContactOverviewOthers.CT = ot.OtherContactList.Count == 0 ? null : ot.OtherContactList[0];
        }

    }
}
